package cn.itcast.p2p20.fragments;


import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.itcast.p2p20.R;
import cn.itcast.p2p20.adapter.HomeRecycleViewAdapter;
import cn.itcast.p2p20.bean.HomeData;
import cn.itcast.p2p20.bean.request.HomeRequest;
import cn.itcast.p2p20.bean.response.HomeResponse;
import cn.itcast.p2p20.net.BaseRequest;
import cn.itcast.p2p20.net.SuccessResultCallback;
import cn.itcast.p2p20.ui.ProgressRing;
import itcast17.lib_arl.AutoRollLayout;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment implements SuccessResultCallback<HomeResponse> {


    @InjectView(R.id.autorolllayout_id)
    AutoRollLayout autorolllayoutId;
    @InjectView(R.id.imageview_advertisement)
    ImageView imageviewAdvertisement;

    @InjectView(R.id.financialrecommend_product_item_layout1)
    LinearLayout financialrecommend_product_item_layout1;
    TextView textview_financialrecommend_produce_name1;
    TextView textview_financialrecommend_produce_annualyield1;
    TextView textview_financialrecommend_produce_days1;
    TextView textview_financialrecommend_produce_price1;

    @InjectView(R.id.financialrecommend_product_item_layout2)
    LinearLayout financialrecommend_product_item_layout2;
    TextView textview_financialrecommend_produce_name2;
    TextView textview_financialrecommend_produce_annualyield2;
    TextView textview_financialrecommend_produce_days2;
    TextView textview_financialrecommend_produce_price2;

    @InjectView(R.id.investmentcass_product_item_layout1)
    RelativeLayout investmentcass_product_item_layout1l;
    TextView textview_investmentcash_produce_name1;
    TextView textview_investmentcash_produce_annualyield1;
    TextView textview_investmentcash_produce_days1;
    ProgressRing investmentcash_raiseprogress1;


    @InjectView(R.id.investmentcass_product_item_layout2)
    RelativeLayout investmentcass_product_item_layout12;
    TextView textview_investmentcash_produce_name2;
    TextView textview_investmentcash_produce_annualyield2;
    TextView textview_investmentcash_produce_days2;
    ProgressRing investmentcash_raiseprogress2;

    @InjectView(R.id.recyclerview_consumptioncredit)
    RecyclerView recyclerview_consumptioncredit;

    @InjectView(R.id.recyclerview_insurance)
    RecyclerView recyclerview_insurance;

    @InjectView(R.id.recyclerview_otherservice)
    RecyclerView recyclerview_otherservice;

    @InjectView(R.id.recyclerview_fundsupermarket)
    RecyclerView recyclerview_fundsupermarket;

    @InjectView(R.id.fundsupermarket_item_layout1)
    LinearLayout fundsupermarket_item_layout11;
    TextView textview_fundSupermarket_name1;
    TextView textview_fundSupermarket_type1;
    TextView textview_fundsupermarket_networth1;
    TextView textview_fundSupermarket_rise1;


    @InjectView(R.id.fundsupermarket_item_layout2)
    LinearLayout fundsupermarket_item_layout12;
    TextView textview_fundSupermarket_name2;
    TextView textview_fundSupermarket_type2;
    TextView textview_fundsupermarket_networth2;
    TextView textview_fundSupermarket_rise2;


    @Override
    protected void initData() {

        sendRequest(new HomeRequest(), HomeResponse.class, this);
    }

    @Override
    protected void initView(View childFragmentView) {

        ButterKnife.inject(this, childFragmentView);
        //月月赢
        textview_financialrecommend_produce_name1 = (TextView) financialrecommend_product_item_layout1.findViewById(R.id.textview_financialrecommend_produce_name);
        textview_financialrecommend_produce_annualyield1 = (TextView) financialrecommend_product_item_layout1.findViewById(R.id.textview_financialrecommend_produce_annualyield);
        textview_financialrecommend_produce_days1 = (TextView) financialrecommend_product_item_layout1.findViewById(R.id.textview_financialrecommend_produce_days);
        textview_financialrecommend_produce_price1 = (TextView) financialrecommend_product_item_layout1.findViewById(R.id.textview_financialrecommend_produce_price);

        //季季赢
        textview_financialrecommend_produce_name2 = (TextView) financialrecommend_product_item_layout2.findViewById(R.id.textview_financialrecommend_produce_name);
        textview_financialrecommend_produce_annualyield2 = (TextView) financialrecommend_product_item_layout2.findViewById(R.id.textview_financialrecommend_produce_annualyield);
        textview_financialrecommend_produce_days2 = (TextView) financialrecommend_product_item_layout2.findViewById(R.id.textview_financialrecommend_produce_days);
        textview_financialrecommend_produce_price2 = (TextView) financialrecommend_product_item_layout2.findViewById(R.id.textview_financialrecommend_produce_price);

        //投资变现
        textview_investmentcash_produce_name1 = (TextView) investmentcass_product_item_layout1l.findViewById(R.id.textview_investmentcash_produce_name);
        textview_investmentcash_produce_annualyield1 = (TextView) investmentcass_product_item_layout1l.findViewById(R.id.textview_investmentcash_produce_annualyield);
        textview_investmentcash_produce_days1 = (TextView) investmentcass_product_item_layout1l.findViewById(R.id.textview_investmentcash_produce_days);
        investmentcash_raiseprogress1 = (ProgressRing) investmentcass_product_item_layout1l.findViewById(R.id.investmentcash_raiseprogress);

        textview_investmentcash_produce_name2 = (TextView) investmentcass_product_item_layout12.findViewById(R.id.textview_investmentcash_produce_name);
        textview_investmentcash_produce_annualyield2 = (TextView) investmentcass_product_item_layout12.findViewById(R.id.textview_investmentcash_produce_annualyield);
        textview_investmentcash_produce_days2 = (TextView) investmentcass_product_item_layout12.findViewById(R.id.textview_investmentcash_produce_days);
        investmentcash_raiseprogress2 = (ProgressRing) investmentcass_product_item_layout12.findViewById(R.id.investmentcash_raiseprogress);

        textview_fundSupermarket_name1 = (TextView) fundsupermarket_item_layout11.findViewById(R.id.textview_fundSupermarket_name);
        textview_fundSupermarket_type1 = (TextView) fundsupermarket_item_layout11.findViewById(R.id.textview_fundSupermarket_type);
        textview_fundsupermarket_networth1 = (TextView) fundsupermarket_item_layout11.findViewById(R.id.textview_fundsupermarket_networth);
        textview_fundSupermarket_rise1 = (TextView) fundsupermarket_item_layout11.findViewById(R.id.textview_fundSupermarket_rise);

        textview_fundSupermarket_name2 = (TextView) fundsupermarket_item_layout12.findViewById(R.id.textview_fundSupermarket_name);
        textview_fundSupermarket_type2 = (TextView) fundsupermarket_item_layout12.findViewById(R.id.textview_fundSupermarket_type);
        textview_fundsupermarket_networth2 = (TextView) fundsupermarket_item_layout12.findViewById(R.id.textview_fundsupermarket_networth);
        textview_fundSupermarket_rise2 = (TextView) fundsupermarket_item_layout12.findViewById(R.id.textview_fundSupermarket_rise);

    }

    @Override
    protected String getTitle() {
        return "首页";
    }

    @Override
    protected int getContentLayoutRes() {
        return R.layout.fragment_home;
    }

    @Override
    public void onSuccessResult(BaseRequest request, HomeResponse response) {

        HomeData homeData = response.data;
        Log.d("HomeFragment", "homeData:" + homeData);

        refreshTopRollImages(homeData.getTopRollImages());
        refreshAdvertisement(homeData.getAdvertisement());

        refreshFinancialRecommend(homeData.getFinancialRecommend());

        refreshInvestmentCash(homeData.getInvestmentCash());


        HomeData.ConsumptionCreditBean consumptionCredit = homeData.getConsumptionCredit();
        LinearLayoutManager layout1 = new LinearLayoutManager(mContext);
        layout1.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview_consumptioncredit.setLayoutManager(layout1);
        recyclerview_consumptioncredit.setAdapter(new HomeRecycleViewAdapter(mContext, consumptionCredit.getImages()));

        HomeData.InsuranceBean insurance = homeData.getInsurance();
        LinearLayoutManager layout2 = new LinearLayoutManager(mContext);
        layout2.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview_insurance.setLayoutManager(layout2);
        recyclerview_insurance.setAdapter(new HomeRecycleViewAdapter(mContext, insurance.getImages()));


        HomeData.OtherServiceBean otherService = homeData.getOtherService();
        LinearLayoutManager layout = new LinearLayoutManager(mContext);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview_otherservice.setLayoutManager(layout);
        recyclerview_otherservice.setAdapter(new HomeRecycleViewAdapter(mContext, otherService.getImages()));

        refreshFundSupermarket(homeData.getFundSupermarket());

    }

    private String FUNDSUPERMARKETTYPE[] = {"混合型", "债券型"};

    //基金超市
    private void refreshFundSupermarket(HomeData.FundSupermarketBean fundSupermarket) {

        LinearLayoutManager layout = new LinearLayoutManager(mContext);
        layout.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerview_fundsupermarket.setLayoutManager(layout);
        recyclerview_fundsupermarket.setAdapter(new HomeRecycleViewAdapter(mContext, fundSupermarket.getImages()));

        List<HomeData.FundSupermarketBean.FundsBean> funds = fundSupermarket.getFunds();

        HomeData.FundSupermarketBean.FundsBean fundsBean1 = funds.get(0);
        textview_fundSupermarket_name1.setText(fundsBean1.getName());
        textview_fundSupermarket_type1.setText(fundsBean1.isIsBond() ? FUNDSUPERMARKETTYPE[0] : FUNDSUPERMARKETTYPE[1]);
        textview_fundsupermarket_networth1.setText(fundsBean1.getNetWorth() + "");
        textview_fundSupermarket_rise1.setText(fundsBean1.getRise() + "");


        HomeData.FundSupermarketBean.FundsBean fundsBean2 = funds.get(1);
        textview_fundSupermarket_name2.setText(fundsBean2.getName());
        textview_fundSupermarket_type2.setText(fundsBean2.isIsBond() ? FUNDSUPERMARKETTYPE[0] : FUNDSUPERMARKETTYPE[1]);
        textview_fundsupermarket_networth2.setText(fundsBean2.getNetWorth() + "");
        textview_fundSupermarket_rise2.setText(fundsBean2.getRise() + "");

    }

    //投资变现
    private void refreshInvestmentCash(HomeData.InvestmentCashBean investmentCash) {

        List<HomeData.InvestmentCashBean.FinancialProductsBean> financialProducts = investmentCash.getFinancialProducts();

        HomeData.InvestmentCashBean.FinancialProductsBean financialProductsBean1 = financialProducts.get(0);
        textview_investmentcash_produce_name1.setText(financialProductsBean1.getName() + "");
        textview_investmentcash_produce_annualyield1.setText(financialProductsBean1.getAnnualYield() + "");
        textview_investmentcash_produce_days1.setText(financialProductsBean1.getDays() + "");
        //设置进度
        investmentcash_raiseprogress1.setProgress(financialProductsBean1.getRaiseProgress());

        HomeData.InvestmentCashBean.FinancialProductsBean financialProductsBean2 = financialProducts.get(1);
        textview_investmentcash_produce_name2.setText(financialProductsBean2.getName() + "");
        textview_investmentcash_produce_annualyield2.setText(financialProductsBean2.getAnnualYield() + "");
        textview_investmentcash_produce_days2.setText(financialProductsBean2.getDays() + "");
        //设置进度
        investmentcash_raiseprogress1.setProgress(financialProductsBean2.getRaiseProgress());

    }

    //理财推荐
    private void refreshFinancialRecommend(HomeData.FinancialRecommendBean financialRecommend) {

        List<HomeData.FinancialRecommendBean.FinancialProductsBean> financialProducts = financialRecommend.getFinancialProducts();
        HomeData.FinancialRecommendBean.FinancialProductsBean financialProductsBean1 = financialProducts.get(0);

        textview_financialrecommend_produce_name1.setText(financialProductsBean1.getName());
        textview_financialrecommend_produce_annualyield1.setText(financialProductsBean1.getAnnualYield() + "");
        textview_financialrecommend_produce_days1.setText(financialProductsBean1.getDays() + "");
        textview_financialrecommend_produce_price1.setText(financialProductsBean1.getPrice() + "");


        HomeData.FinancialRecommendBean.FinancialProductsBean financialProductsBean2 = financialProducts.get(1);

        textview_financialrecommend_produce_name2.setText(financialProductsBean2.getName());
        textview_financialrecommend_produce_annualyield2.setText(financialProductsBean2.getAnnualYield() + "");
        textview_financialrecommend_produce_days2.setText(financialProductsBean2.getDays() + "");
        textview_financialrecommend_produce_price2.setText(financialProductsBean2.getPrice() + "");

    }

    private void refreshAdvertisement(String advertisement) {

        Picasso.with(mContext).load(advertisement).into(imageviewAdvertisement);
    }

    //轮播图
    private void refreshTopRollImages(List<String> topRollImages) {

        autorolllayoutId.setItems(topRollImages);

    }

}
